package com.yuxin.tongyi.lsll.select;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.yuxin.tongyi.lsll.R;
import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.pojo.ClassInfo;
import com.yuxin.tongyi.lsll.pojo.ClassObj;
import com.yuxin.tongyi.lsll.sub.DayLifeActivity;
import com.yuxin.tongyi.lsll.thread.GetClassDataThread;
import com.yuxin.tongyi.lsll.util.PubUtil;

/**
 * @author ww
 * @date:2012-4-6 上午08:52:25
 * @Desc: 班级选择界面
 * @version :
 * 
 */
public class SelectClassActivity extends Activity {
	private static final String TAG = "SelectClassActivity";
	private ListView listView;
	private ClassAdapter adapter;
	private GetClassDataThread mClassThread; // 班级列表线程
	private MyHandler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_class);

		initView();

		PubUtil.showProgressDialog(SelectClassActivity.this, Constants.MSG_LOADING, Constants.MSG_WAIT);
		mHandler = new MyHandler();
		mClassThread = new GetClassDataThread(this, mHandler);
		new Thread(mClassThread).start();
	}

	public void initView() {
		listView = (ListView) findViewById(R.id.select_class_lvclass);
		listView.setOnItemClickListener(new onItemClick());
	}
	
	class onItemClick implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			String CurrentClassId = ((TextView) view.findViewById(R.id.select_class_tvitemid)).getText().toString();
			String CurrentClassName = ((TextView) view.findViewById(R.id.select_class_tvitem)).getText().toString();
			
			Intent intent = new Intent(SelectClassActivity.this, DayLifeActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString(Constants.SELECT_CLASS_ID, CurrentClassId);  
			bundle.putString(Constants.SELECT_CLASS_NAME, CurrentClassName); 
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finish();
		}
	}
	
	public final class ViewHolder {
		public TextView tvid;
		public TextView tvname;
	}

	class ClassAdapter extends BaseAdapter {

		private Context _context;
		private List<ClassObj> mList;
		private LayoutInflater inflater;

		ClassAdapter(Context context, List<ClassObj> list) {
			this._context = context;
			this.mList = list;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			ViewHolder holder = null;
			if (view == null) {
				holder = new ViewHolder();
				view = inflater.inflate(R.layout.select_class_item, null);
				holder.tvid = (TextView) view.findViewById(R.id.select_class_tvitemid);
				holder.tvname = (TextView) view.findViewById(R.id.select_class_tvitem);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}
			
			holder.tvid.setText(mList.get(position).getClassId());
			holder.tvname.setText(mList.get(position).getClassName());

			return view;
		}
	}

	private class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			switch (msg.what) {
			// 读取CLASS成功
			case Constants.TH_CLASS_ON:
				ClassInfo classInfo = (ClassInfo) bundle.getSerializable(Constants.STREAM_CLASS_OBJECT);
				setClassData(classInfo);
				PubUtil.closeProgressDialog();
				break;
			// 读取CLASS失败，加载缓存
			case Constants.TH_CLASS_OFF:
				PubUtil.showToastLong(SelectClassActivity.this, Constants.MSG_READCACHE);
				ClassInfo classInfo1 = (ClassInfo) bundle.getSerializable(Constants.STREAM_CLASS_OBJECT);
				setClassData(classInfo1);
				PubUtil.closeProgressDialog();
				break;
			}
		}
	}

	private void setClassData(ClassInfo info) {
		List<ClassObj> list = info.getClassObj();

		adapter = new ClassAdapter(this, list);
		listView.setAdapter(adapter);
	}
}
